package gov.pbc.gx.nn.pz.dao;

import gov.pbc.gx.nn.pz.entity.Account;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bstek.bdf2.core.business.IDept;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.data.variant.Record;

public abstract class BaseImageDao extends AbstractHibernateDao {


	public void drawTick(Graphics2D graphics, int x, int y) {
		BasicStroke stroke = new BasicStroke(2, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_BEVEL);
		graphics.setStroke(stroke);
		Line2D line1 = new Line2D.Double(x - 3, y - 5, x, y);
		graphics.draw(line1);
		Line2D line2 = new Line2D.Double(x, y, x + 7, y - 7);
		graphics.draw(line2);
	}

	public PzFont getFont(Integer pzcode) {
		String hql = " from PzFont where invalid=false and  createUser=:createUser ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("createUser", getLoginUserName());
		List<PzFont> pzfonts = this.query(hql, param);
		if (!pzfonts.isEmpty()) {
			return pzfonts.get(0);
		} else {
			PzFont pzFont = new PzFont();
			pzFont.setCnFont("宋体");
			pzFont.setNumFont("微软雅黑");
			return pzFont;
		}

	}

	// 子类中共用的方法下面都是
	/*
	 * 得到所有的字体
	 */
	@DataProvider
	public List<Record> getFonts() {
		String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getAvailableFontFamilyNames();
		List<Record> recordfonts = new ArrayList<Record>();
		for (String string : fonts) {
			Record recordfont = new Record();
			recordfont.put("fontfamilyname", string);
			recordfonts.add(recordfont);
		}
		Collections.reverse(recordfonts);
		return recordfonts;
	}

	/*
	 * 得到部门id
	 */
	public String getDeptId() {
		IUser loginuser = ContextHolder.getLoginUser();
		List<IDept> depts = loginuser.getDepts();
		String deptid = null;
		if (depts == null || depts.isEmpty()) {
		} else {
			deptid = depts.get(0).getId();
		}
		return deptid;

	}

	public String getLoginUserName() {
		IUser loginuser = ContextHolder.getLoginUser();
		String loginName = loginuser.getUsername();
		return loginName;

	}

	/*
	 * 根据pzcode更新字体的方法，
	 */
	public void updateFontBase(PzFont pzfont, Integer pzcode) {
		String hql = "select count(*) from " + PzFont.class.getName()
				+ " where  createUser=:createUser";
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("createUser",getLoginUserName());
		if (this.queryForInt(hql,map) == 0) {
			pzfont.setId(null);
//			pzfont.setPzCode(pzcode);
			pzfont.setCreateUser(getLoginUserName());
			this.save(pzfont);
		} else {
//			pzfont.setPzCode(pzcode);
			this.update(pzfont);
		}
	}

	/*
	 * 根据pzcode保存所有的xy；
	 */
	public void saveXjpzXYBase(Collection<HcpzXY> hcxjpzxys, Integer pzcode) {
		String hql = "select count(*)  from HcpzXY where pzCode=:pzcode and createUser=:createUser";
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("pzcode",pzcode);
		map.put("createUser",getLoginUserName());
		
		if (this.queryForInt(hql,map) == 0) {
			for (HcpzXY xjpzXY : hcxjpzxys) {
				xjpzXY.setId(null);
				xjpzXY.setPzCode(pzcode);
				xjpzXY.setCreateUser(getLoginUserName());
				this.save(xjpzXY);
			}
		} else {
			for (HcpzXY xjpzXY : hcxjpzxys) {
				xjpzXY.setPzCode(pzcode);
				this.update(xjpzXY);
			}
		}
	}

	/*
	 * 根据pzcode得到xy的坐标
	 */
	public List<HcpzXY> getHcPzXYsBase(Integer pzcode) {
		String hql = " from HcpzXY where pzCode=:pzCode and createUser=:createUser and invalid=false";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("pzCode", pzcode);
		param.put("createUser", getLoginUserName());
		List<HcpzXY> hcxjpzxys = this.query(hql, param);
		return hcxjpzxys;
	}

	// 跟据賬號得到名字
	public Account getAccountName(String account) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("account", account);
		String hql = "from Account where account=:account and invalid=false";
		List<Account> accounts = this.query(hql, map);
		if (!accounts.isEmpty()) {
			return accounts.get(0);
		} else {
			return null;
		}
	}
}
